package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.qo.QueryObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper ;
    @Autowired
    private RoleMapper roleMapper ;
    @Autowired
    private RequestMappingHandlerMapping handlerMapping;

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
        //permissionMapper.deleteEmpANdRole(id);
    }

    @Override
    public void update(Permission permission,Long[] ids) {
        permissionMapper.updateByPrimaryKey(permission);
        //修改时把之前的角色关系全部删掉
        //permissionMapper.deleteEmpANdRole(permission.getId());
        //删完之后在添加新的角色关系
        if (ids!=null){
            //为中间表添加关系
            for (Long id : ids) {
                //permissionMapper.insertEmpANdRole(permission.getId(), id);
            }
        }
    }

    @Override
    public void insert(Permission permission,Long[] ids) {
        permissionMapper.insert(permission);
        if (ids!=null){
            //为中间表添加关系
            for (Long id : ids) {
                //permissionMapper.insertEmpANdRole(permission.getId(), id);
            }
        }
    }

    @Override
    public Permission selectByPrimaryKey(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> selectAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());

        return new PageInfo<Permission>(permissionMapper.selectPermission(qo));
    }

    @Override
    public List<Permission> selectEmpAndPermission(Long empId) {
        return null;
    }

    @Override
    public void reload() {
        //进行权限重新加载
        //首先获取数据库中存在的权限数据 获取全部的权限表达式 以便后面对比去重
        List<String> expressionAll = permissionMapper.selectExpression();
        //用SpringMVC获取全部方法
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
        System.out.println(handlerMethods);
        //获取map集合中所有的value
        Collection<HandlerMethod> values = handlerMethods.values();
        for (HandlerMethod m : values) {
            //判断方法上有没有RequiredPermission这个注解
            RequiredPermission methodAnnotation = m.getMethodAnnotation(RequiredPermission.class);
            if (methodAnnotation != null){
                String name = methodAnnotation.name();
                String expression = methodAnnotation.expression();
                //判断数组中有没有存在这个
                if( !expressionAll.contains(expression)){
                    //不存在就写进数据库
                    Permission permission = new Permission();
                    permission.setName(name);
                    permission.setExpression(expression);
                    permissionMapper.insert(permission);
                    //当添加进去之后就给集合加元素,断绝重复
                    expressionAll.add(expression);
                }

            }
        }
    }
}
